package com.gjy.concurrency.beautiful;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-06-13 09:07:35
 */
public class AtomicTest {

    private static final Logger log = LoggerFactory.getLogger(AtomicTest.class);

    @Test
    public void test1() throws InterruptedException {
        AtomicInteger atomic = new AtomicInteger(0);

        int[] a1 = {1, 2, 1, 1, 0, 3, 1, 1};
        int[] a2 = {1, 2, 1, 1, 0, 3, 1, 1};

        ExecutorService pool = Executors.newFixedThreadPool(2);

        pool.execute(() -> {
            for (int i : a1) {
                if (i == 1) {
                    atomic.incrementAndGet();
                }
            }
        });

        pool.execute(() -> {
            for (int i : a2) {
                if (i == 1) {
                    atomic.incrementAndGet();
                }
            }
        });

        TimeUnit.SECONDS.sleep(1);
        log.info("get = {}", atomic.get());
        pool.shutdown();
    }

}
